package com.yinquan.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yishun.springboot.dao.CWRServiceTest;

import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 本类是封装或者说明fastjson转换使用的类
 * @author YS
 *
 */

@Slf4j
public class Object2str {

	
	
	/**
	 * JSONArray jsonArray=jsonObject.getJSONObject("data").getJSONArray("fileList").getJSONObject(0).getJSONArray("labels");  复杂json获取对象
	 */
	
	
	/**          fastjson转换各种对象
	/*String info = HttpUtil.post(url, json);
	 * JSONObject jsonObject = JSON.parseObject(info);       //将字符串转换为JSONObject,只用这一行代码，JSONObject是等同于HashMap或者Map的
		String a =jsonObject.getString("result_data");
		JSONObject jsonObject2 = JSON.parseObject(a);           //这个和上面一样
		String a2 = jsonObject2.getString("dict_list");
		JSONArray jsonObject3 = JSON.parseArray(a2);            //将字符串转换为JSONArray，只用这一行代码，好像和List或者ArrayList也是一样的
		String a3 = jsonObject3.get(0).toString(); 
		GwsDictListJobName d = JSON.parseObject(a3,GwsDictListJobName.class);        //将字符串转换为对象
		
		JSONObject jsonObject;
		TspHaveDevCode tspHaveDevCode = JSONObject.toJavaObject(jsonObject, TspHaveDevCode.class);
		/将JSONObject转为对象，只用这一行代码
		
		Data data = new Data();                                                    
		String s = JSON.toJSONString(data);                                        //对象转json
		
		String body2="";//json here 
			body2 = body2.replaceAll("\\\"","");               //将json字符串转为无双引号json
			
		data.setSuborderNo("58961");
        JSONObject jsonObj = (JSONObject) JSON.toJSON(data);     //java bean转为JSONObject
	 * @param list
	 * @return
	 */
	
	
	
	
	
	
	
	
	public String list2str(List<?> list) {
		JSONArray jsonArray = (JSONArray) JSONArray.toJSON(list);
		//log.info(jsonArray.toString());
		return jsonArray.toJSONString(); // 返回String
	}

	public List<String> str2list(String string) {

		List<String> lis = Arrays.asList(string.split(","));
		return lis; // 返回String的List 此两方法为处理后台能够返回一个String类型的List集合到前端，能够让前端js做处理请求
	}
	

	public String map2str(Map map) {
		 String str1 = getMapToString(map);
		return str1; // 返回String的List 此两方法为处理后台能够返回一个String类型的List集合到前端，能够让前端js做处理请求
	}

	//好像不能用
	public static String getMapToString(Map<String,Object> map){
	       Set<String> keySet = map.keySet();
	       //将set集合转换为数组
	       String[] keyArray = keySet.toArray(new String[keySet.size()]);
	       //给数组排序(升序)
	       Arrays.sort(keyArray);
	       //因为String拼接效率会很低的，所以转用StringBuilder
	       StringBuilder sb = new StringBuilder();
	       for (int i = 0; i < keyArray.length; i++) {
	           // 参数值为空，则不参与签名 这个方法trim()是去空格
	           if ((String.valueOf(map.get(keyArray[i]))).trim().length() > 0) {
	               sb.append(keyArray[i]).append(":").append(String.valueOf(map.get(keyArray[i])).trim());
	           }
	           if(i != keyArray.length-1){
	               sb.append(",");
	           }
	       }
	       return sb.toString();
	   }
	
	
	private void putMap(String... args) {
		// TODO Auto-generated method stub

		Map M = new HashMap();
		Map qitaM = new HashMap();
		Map qitaSm = new HashMap();
		
		for (int i = 0; i < args.length; i++) {
			ArrayList<String>arrayList = new ArrayList<>();
			for (int j = 0; j < args.length; j++) {
				log.info("fir");
				arrayList.add(args[i]);
				
			}
			
			Map temp = new HashMap();
			for (int j = 0; j < args.length; j++) {
				log.info("sec");
				qitaSm.put("title",arrayList.get(j));
				qitaSm.put("href", arrayList.get(j));
				qitaSm.put("icon", arrayList.get(j));
				qitaM.put("title", arrayList.get(j));
				qitaM.put("icon", arrayList.get(j));
				qitaM.put("href", arrayList.get(j));
				qitaM.put("spread", arrayList.get(j));
				qitaM.put("children",arrayList.get(j));
				
			}
			
			
			
		}
	}
	public static void main(String[] args) throws Exception {
		String a = "{'action':'asdas','id':'12'}";
		JSONObject jsonObject = jsonToJsonBean(a) ;
		log.info(jsonObject.getString("action"));
		
	}
	
	
	public static JSONObject jsonToJsonBean(String string) {
 
        return JSON.parseObject(string);
        
    }
	
	/**
	 * map迭代方法
	 * @param map
	 */
	private void dedaimapfangfa(Map map) {
		// TODO Auto-generated method stub
		for (Object s: map.keySet()) {

   		 log.info(s +"的对应值为:" + map.get(s));

   		  }
	}
//字符串转ASCII
	public static String stringToAscii(String value)  
	{  
	    StringBuffer sbu = new StringBuffer();  
	    char[] chars = value.toCharArray();   
	    for (int i = 0; i < chars.length; i++) {  
	        if(i != chars.length - 1)  
	        {  
	            sbu.append((int)chars[i]).append(",");  
	        }  
	        else {  
	            sbu.append((int)chars[i]);  
	        }  
	    }  
	    return sbu.toString();  
	}  
	
	//ASCII转字符串
	public  String asciiToString(String value)  
	{  
	    StringBuffer sbu = new StringBuffer();  
	    String[] chars = value.split(",");  
	    for (int i = 0; i < chars.length; i++) {  
	        sbu.append((char) Integer.parseInt(chars[i]));  
	    }  
	    return sbu.toString();  
	}  
	
}
